package com.ruoyi.web.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.mes.MesWarehousingStatement;
import com.ruoyi.web.domain.mes.MesWarehousingStatementDetail;
import com.ruoyi.web.mapper.DeliveryMapper;
import com.ruoyi.web.service.MesWarehousingStatementDetailService;
import com.ruoyi.web.service.MesWarehousingStatementService;
import com.ruoyi.web.vo.Delivery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/warehousing/statement")
public class MesWarehousingStatementController extends BaseController {
    @Resource
    private MesWarehousingStatementService mesWarehousingStatementService;
    @Resource
    private MesWarehousingStatementDetailService mesWarehousingStatementDetailService;
    @Resource
    private DeliveryMapper deliveryMapper;

    @GetMapping("/list")
    public TableDataInfo list(MesWarehousingStatement statement){
        startPage();
        List<MesWarehousingStatement> list = mesWarehousingStatementService.list(new QueryWrapper<MesWarehousingStatement>()
                .eq(StringUtils.isNotBlank(statement.getStatementNo()), "statement_no", statement.getStatementNo())
                .ge(StringUtils.isNotBlank(statement.getBeginDate()), "statement_time", statement.getBeginDate() + " 00:00:00")
                .le(StringUtils.isNotBlank(statement.getEndDate()), "statement_time", statement.getEndDate() + " 23:59:59")
        );
        return getDataTable(list);
    }

    @GetMapping("/getById")
    public AjaxResult getById(MesWarehousingStatement statement){
        if(statement.getPid() == null){
            return AjaxResult.error("未获取到主键信息");
        }
        List<MesWarehousingStatement> list = mesWarehousingStatementService.list(new QueryWrapper<MesWarehousingStatement>()
                .eq("pid", statement.getPid())
        );
        MesWarehousingStatement statement1 = list.get(0);
        List<MesWarehousingStatementDetail> statementDetails = mesWarehousingStatementDetailService.list(new QueryWrapper<MesWarehousingStatementDetail>().eq("statement_id", statement1.getPid()));
        statement1.setStatementDetails(statementDetails);
        return AjaxResult.success(statement1);
    }

    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody MesWarehousingStatement statement){
        if(statement.getStatementDetails() == null || statement.getStatementDetails().size() == 0){
            return AjaxResult.error("未获取到明细信息");
        }
        Date date = new Date();
        String username = SecurityUtils.getUsername();
        statement.setCreateBy(username);
        statement.setCreateTime(date);
        mesWarehousingStatementService.save(statement);
        statement.getStatementDetails().forEach(e ->{
            e.setStatementId(statement.getPid());
            if(StringUtils.isBlank(e.getDeliveryDetailId())) throw new BaseException("未获取到关联的发货单细表ID");
        });
        mesWarehousingStatementDetailService.saveBatch(statement.getStatementDetails());
        return AjaxResult.success();
    }

    // 新增/修改选择生产单时，根据发货单查询需要的信息
    @GetMapping("/getProduceOrder")
    public TableDataInfo getProduceOrder(Delivery delivery){
        List<Delivery> deliveries = deliveryMapper.selectProduceOrder(delivery);
        return getDataTable(deliveries);
    }

    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody MesWarehousingStatement statement){
        if(statement.getPid() == null){
            return AjaxResult.error("未获取到主键信息");
        }
        if(statement.getStatementDetails() == null || statement.getStatementDetails().size() == 0){
            return AjaxResult.error("未获取到明细信息");
        }
        statement.getStatementDetails().forEach(e ->{
            e.setStatementId(statement.getPid());
            if(StringUtils.isBlank(e.getDeliveryDetailId())) throw new BaseException("未获取到关联的发货单细表ID");
        });
        Date date = new Date();
        String username = SecurityUtils.getUsername();
        statement.setUpdateBy(username);
        statement.setUpdateTime(date);
        mesWarehousingStatementService.updateById(statement);
        // 删除历史数据
        mesWarehousingStatementDetailService.remove(new QueryWrapper<MesWarehousingStatementDetail>()
                .eq("statement_id", statement.getPid()));
        // 添加新数据
        mesWarehousingStatementDetailService.saveBatch(statement.getStatementDetails());
        return AjaxResult.success();
    }

    @PostMapping("/del")
    @Transactional
    public AjaxResult del(@RequestBody MesWarehousingStatement statement){
        if(statement.getPid() == null){
            return AjaxResult.error("未获取到主键信息");
        }
        mesWarehousingStatementService.removeById(statement.getPid());
        mesWarehousingStatementDetailService.remove(new QueryWrapper<MesWarehousingStatementDetail>()
                .eq("statement_id", statement.getPid()));
        return AjaxResult.success();
    }
}
